<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>奉孝的测开日记</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  
  
  
    <link rel="alternate" href="/blog/atom.xml" title="奉孝的测开日记" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/blog/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/blog/css/style.css">

  
    
<link rel="stylesheet" href="/blog/fancybox/jquery.fancybox.min.css">

  
  
    
<link rel="stylesheet" href="/blog/localshare/css/share.css">

  
  
  
<meta name="generator" content="Hexo 5.4.2"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/blog/" id="logo">奉孝的测开日记</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/blog/" id="subtitle">开启自我改变的原动力</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        
          <a class="main-nav-link" href="/blog/."><i class="fa fa-home"></i> 首页</a>
        
          <a class="main-nav-link" href="/blog/archives/"><i class="fa fa-archive"></i> 归档</a>
        
          <a class="main-nav-link" href="/blog/about/"><i class="fa fa-user"></i> 关于</a>
        
      </nav>
    </div>
    <div id="search-form">
      <div id="result-mask" class="hide"></div>
      <label><input id="search-key" type="text" autocomplete="off" placeholder="搜索"></label>
      <div id="result-wrap" class="hide">
        <div id="search-result"></div>
      </div>
      <div class="hide">
        <template id="search-tpl">
          <div class="item">
            <a href="/{path}" title="{title}">
              <div class="title">{title}</div>
              <div class="time">{date}</div>
              <div class="tags">{tags}</div>
            </a>
          </div>
        </template>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-深入理解列表和元组" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-inner">
    
    
    <div class="article-meta">
      
      <span class="article-date">
  <i class="fa fa-date"></i>
  <time class="dt-published" datetime="2023-03-14T09:14:39.814Z" itemprop="datePublished">2023年03月14日</time>
</span>
      
      
        <span class="article-views">
  <i class="fa fa-views"></i>
  <i id="busuanzi_container_page_pv">
      <i id="busuanzi_value_page_pv"></i>
  </i>
</span>

      
      
<a href="/blog/2023/03/14/%E6%B7%B1%E5%85%A5%E7%90%86%E8%A7%A3%E5%88%97%E8%A1%A8%E5%92%8C%E5%85%83%E7%BB%84/#comments" class="article-comment-link">
  
    
    
    
    
    
  
  <i class="fa fa-commt"></i>
  留言
</a>


    </div>
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h1 id="列表和元组基础"><a href="#列表和元组基础" class="headerlink" title="列表和元组基础"></a>列表和元组基础</h1><p><strong>基本概念</strong></p>
<p>一个可以放任意数据类型的有序集合。</p>
<p>Java编程中集合的数据类型必须一致，而Python就比较随意</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">l = [<span class="string">&quot;小红&quot;</span>, <span class="number">1</span>]</span><br><span class="line">tup = (<span class="string">&#x27;小红&#x27;</span>, <span class="number">1</span>)</span><br></pre></td></tr></table></figure>

<p><strong>区别</strong></p>
<ul>
<li>列表是动态的，长度大小不固定，可以随意地增加、删减或者改变元素</li>
<li>元组是静态的，长度大小固定，无法增加删减或者改变</li>
</ul>
<p><strong>相比Java便捷操作</strong></p>
<ul>
<li><p>支持负数索引，-1表示最后一个元素。Java用负数索引会报错</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java.lang.ArrayIndexOutOfBoundsException</span><br></pre></td></tr></table></figure></li>
<li><p>切片操作</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">l = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line">l[<span class="number">0</span>:<span class="number">1</span>]</span><br></pre></td></tr></table></figure></li>
</ul>
<p><strong>常用内置函数</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">l = [<span class="number">3</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">1</span>]</span><br><span class="line">l.count(<span class="number">3</span>) <span class="comment"># 2 item 出现的次数</span></span><br><span class="line"></span><br><span class="line">l.index(<span class="number">7</span>) <span class="comment"># 3 item 第一次出现的索引</span></span><br><span class="line"></span><br><span class="line">l.reverse() <span class="comment"># [1, 8, 7, 3, 2, 3] 原地倒转列表,元组没有此函数</span></span><br><span class="line"></span><br><span class="line">l.sort() <span class="comment"># [1, 2, 3, 3, 7, 8] 排序,元组没有此函数 </span></span><br><span class="line"></span><br><span class="line">tup = (<span class="number">3</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">1</span>)</span><br><span class="line">tup.count(<span class="number">3</span>)</span><br><span class="line"></span><br><span class="line">tup.index(<span class="number">7</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">list</span>(<span class="built_in">reversed</span>(tup)) <span class="comment"># [1, 8, 7, 3, 2, 3] 对列表/元组进行倒转，reversed()返回一个倒转后的迭代器（例子使用 list() 函数再将其转换为列表）</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">sorted</span>(tup) <span class="comment"># [1, 2, 3, 3, 7, 8] 返回排好序的新列表</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h1 id="深入解析"><a href="#深入解析" class="headerlink" title="深入解析"></a>深入解析</h1><h2 id="python这么随意吗"><a href="#python这么随意吗" class="headerlink" title="python这么随意吗"></a>python这么随意吗</h2><p>基础模块中，提到同一个列表中可以存储不同数据类型的元素。</p>
<p>那么为什么Python可以如此随意呢？翻源码<a target="_blank" rel="noopener" href="https://github.com/python/cpython/blob/main/Include/cpython/listobject.h">https://github.com/python/cpython/blob/main/Include/cpython/listobject.h</a></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> _<span class="title">object</span> &#123;</span></span><br><span class="line">    _PyObject_HEAD_EXTRA  <span class="comment">/* 构造双向链表*/</span></span><br><span class="line">    Py_ssize_t ob_refcnt; <span class="comment">/* 引用计数器 */</span></span><br><span class="line">    PyTypeObject *ob_type;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> &#123;</span></span><br><span class="line">    PyObject ob_base;</span><br><span class="line">    Py_ssize_t ob_size; <span class="comment">/* 内部元素个数 */</span></span><br><span class="line">&#125; PyVarObject;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> &#123;</span></span><br><span class="line">    PyObject_VAR_HEAD</span><br><span class="line">    <span class="comment">/* 指向列表元素的指针向量.  list[0] is ob_item[0], etc. */</span></span><br><span class="line">    PyObject **ob_item;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/* ob_item contains space for &#x27;allocated&#x27; elements.  The number</span></span><br><span class="line"><span class="comment">     * currently in use is ob_size.</span></span><br><span class="line"><span class="comment">     * Invariants:</span></span><br><span class="line"><span class="comment">     *     0 &lt;= ob_size &lt;= allocated</span></span><br><span class="line"><span class="comment">     *     len(list) == ob_size</span></span><br><span class="line"><span class="comment">     *     ob_item == NULL implies ob_size == allocated == 0</span></span><br><span class="line"><span class="comment">     * list.sort() temporarily sets allocated to -1 to detect mutations.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * Items must normally not be NULL, except during construction when</span></span><br><span class="line"><span class="comment">     * the list is not yet visible outside the function that builds it.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    Py_ssize_t allocated;</span><br><span class="line">&#125; PyListObject;</span><br></pre></td></tr></table></figure>

<p>看源码知道，PyListObject结构体定义了类型为</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">PyObject **</span><br></pre></td></tr></table></figure>

<p>的ob_item，指针指向了元素列表所在内存块的首地址。所以python的list并不直接保存元素，而是保存的指针，这也是list中可以同时存放多种类型元素的根本原因。</p>
<p>通俗点讲，列表和元组本质上可以看成是一个指针列表，里面存放了每个元素的地址，而这些地址指向的具体元素，类型和大小可能是不同的，正是这种机制实现了不同数据类型元素的存储，而存储的每个指针变量都占8字节，所以可以这么理解，本质上，每次插入元素大小是固定的，不管字符串、整型，因为存储的都是指针变量。</p>
<h2 id="列表和元组存储差异"><a href="#列表和元组存储差异" class="headerlink" title="列表和元组存储差异"></a>列表和元组存储差异</h2><p>基础模块中提到，列表是动态可变的，元组是静态不可变的，那么存储有何差异呢</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">l = []</span><br><span class="line">l.__sizeof__() #</span><br><span class="line"></span><br><span class="line">1、若直接购买正式会员（年卡、两年卡、分销1年赠1卡），不再展示 9.9元体验卡</span><br><span class="line">2、机构介绍机构，扫码后跳转到机构注册页</span><br><span class="line">3、机构介绍用户，扫码后跳转到9.9元支付页</span><br><span class="line">4、机构介绍机构，若无绑定关系则添加绑定关系</span><br><span class="line">5、同一个身份证号只能填写一次</span><br><span class="line">用户获得过会员时长之后不再绑定介绍关系（合伙人与用户之间）</span><br><span class="line"></span><br></pre></td></tr></table></figure>


        
            <div id="toc-article">
                
  <div class="widget-wrap" id="toc-wrap">
    <h3 class="widget-title"><i class="fa fa-toc"></i> 文章目录</h3>
    <div class="widget">
      <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%88%97%E8%A1%A8%E5%92%8C%E5%85%83%E7%BB%84%E5%9F%BA%E7%A1%80"><span class="toc-text">列表和元组基础</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%B7%B1%E5%85%A5%E8%A7%A3%E6%9E%90"><span class="toc-text">深入解析</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#python%E8%BF%99%E4%B9%88%E9%9A%8F%E6%84%8F%E5%90%97"><span class="toc-text">python这么随意吗</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%97%E8%A1%A8%E5%92%8C%E5%85%83%E7%BB%84%E5%AD%98%E5%82%A8%E5%B7%AE%E5%BC%82"><span class="toc-text">列表和元组存储差异</span></a></li></ol></li></ol>
    </div>
  </div>


            </div>
        
        
          <blockquote id="copyright">
              <p>原文链接: <a href="http://haohongbin.gitee.io/blog/2023/03/14/深入理解列表和元组/">http://haohongbin.gitee.io/blog/2023/03/14/深入理解列表和元组/</a></p>
              <p>版权声明: 转载请注明出处.</p>
          </blockquote>
        
      
    </div>
    <footer class="article-footer">
      
        <div class="article-tag-wrap">
          

          
          
    <div class="social-share">
      <span>分享到:</span>
    </div>



        </div>
      
      
        
<nav id="article-nav">
  
    <a href="/blog/2023/03/09/Java%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95Mock%E7%A5%9E%E5%99%A8/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">older</strong>
      <div class="article-nav-title">
        
          Java单元测试Mock神器
        
      </div>
    </a>
  
  
</nav>

      
      
        








      
    </footer>
  </div>
</article>
</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title"><i class="fa fa-posts"></i> 最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/blog/2023/03/14/%E6%B7%B1%E5%85%A5%E7%90%86%E8%A7%A3%E5%88%97%E8%A1%A8%E5%92%8C%E5%85%83%E7%BB%84/">(no title)</a>
          </li>
        
          <li>
            <a href="/blog/2023/03/09/Java%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95Mock%E7%A5%9E%E5%99%A8/">Java单元测试Mock神器</a>
          </li>
        
          <li>
            <a href="/blog/2023/02/21/Pytest%E6%89%A7%E8%A1%8C%E7%94%A8%E4%BE%8B%E7%9A%84%E5%A4%9A%E7%A7%8D%E6%96%B9%E5%BC%8F/">Pytest执行用例的多种方式</a>
          </li>
        
          <li>
            <a href="/blog/2023/02/20/SQL%E6%9F%A5%E8%AF%A2%E5%9C%BA%E6%99%AF/">SQL查询场景</a>
          </li>
        
          <li>
            <a href="/blog/2023/01/18/MySQL%E5%91%BD%E4%BB%A4%E6%89%8B%E5%86%8C/">MySQL命令手册</a>
          </li>
        
      </ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title"><i class="fa fa-tag"></i> 标签云</h3>
    <div class="widget tagcloud">
      <a href="/blog/tags/MySQL/" style="font-size: 15px;">MySQL</a> <a href="/blog/tags/%E5%88%86%E4%BA%AB/" style="font-size: 15px;">分享</a> <a href="/blog/tags/%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95/" style="font-size: 10px;">单元测试</a> <a href="/blog/tags/%E5%AD%A6%E4%B9%A0%E6%80%BB%E7%BB%93/" style="font-size: 20px;">学习总结</a> <a href="/blog/tags/%E8%87%AA%E5%8A%A8%E5%8C%96/" style="font-size: 10px;">自动化</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title"><i class="fa fa-classify"></i> 分类</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Java/">Java</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Java%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF/">Java核心技术</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/hexo%E5%8D%9A%E5%AE%A2/">hexo博客</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/vim/">vim</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/%E5%90%8E%E7%AB%AF/">后端</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/%E6%B5%8B%E8%AF%95/">测试</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/%E6%B5%8B%E8%AF%95%E8%BF%9B%E9%98%B6/">测试进阶</a><span class="category-list-count">1</span></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title"><i class="fa fa-archive"></i> 归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2023/">2023年</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2022/">2022年</a><span class="archive-list-count">10</span></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title"><i class="fa fa-tag"></i> 标签</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/MySQL/" rel="tag">MySQL</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/%E5%88%86%E4%BA%AB/" rel="tag">分享</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95/" rel="tag">单元测试</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/%E5%AD%A6%E4%B9%A0%E6%80%BB%E7%BB%93/" rel="tag">学习总结</a><span class="tag-list-count">7</span></li><li class="tag-list-item"><a class="tag-list-link" href="/blog/tags/%E8%87%AA%E5%8A%A8%E5%8C%96/" rel="tag">自动化</a><span class="tag-list-count">1</span></li></ul>
    </div>
  </div>


  
    

  
</aside>
        
      </div>
      <a id="totop" href="#top"></a>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      <p>
        <a href="/blog/sitemap.xml">网站地图</a>
        <span> | </span><a href="/blog/atom.xml">订阅本站</a>
        <span> | </span><a href="/blog/about/">联系博主</a>
      </p>
      
        <p>
          <i class="fa fa-visitors"></i>
          <i id="busuanzi_container_site_uv"><i id="busuanzi_value_site_uv"></i></i>
          ，
          <i class="fa fa-views"></i>
          <i id="busuanzi_container_site_pv"><i id="busuanzi_value_site_pv"></i></i>
        </p>
      
      <p>
        <span>Copyright &copy; 2023 奉孝.</span>
        <span>Theme by <a href="https://github.com/chaooo/hexo-theme-BlueLake/" target="_blank">BlueLake.</a></span>
        <span>Powered by <a href="https://hexo.io/" target="_blank">Hexo.</a></span>
      </p>
    </div>
  </div>
</footer>

    </div>
  </div>
  
<script src="/blog/js/jquery-3.4.1.min.js"></script>


<script src="/blog/js/search.json.js"></script>


  
<script src="/blog/fancybox/jquery.fancybox.min.js"></script>




<script src="/blog/js/script.js"></script>






  
<script src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




  
    
<script src="/blog/localshare/js/social-share.js"></script>

    
<script src="/blog/localshare/js/qrcode.js"></script>

  
  



  

  

  

  

  

  

  

  
  





</body>
</html>